# 3.6 AD Button ## 3.6.1 Overview The AD button requires only one analog pin to read multiple button states, which greatly saves IO ports. It adopts analog acquisition, and the output voltages vary from pressed buttons, so that different analog values can be obtained. We can determine which button is pressed according to these values. ## 3.6.2 Schematic Diagram ![t75](./media/t75.png) From the Schematic Diagram: When no button is pressed, the output at signal pin IO33 is pulled down by R34 (connected to GND. So the analog value of IO33 is 0, that is, low level 0V; When button S1(the red button) is pressed, pin IO33 is connected to VCC. So the analog value of IO33 is 4095 (voltage = 3.3V). When button S2(the yellow button) is pressed, the voltage of IO33 is that between R32 and R34: VCCxR34/(R32+R34) ≈ 2.2V, and the analog value is about 2432; When button S3(the green button) is pressed, the voltage of IO33 is that between R32+R33 and R34: VCCxR34/(R32+R33+R34) ≈ 1.1V, and the analog value is about 1175. ## 3.6.3 Code Blocks Blocks in ![](./media/codingBox.png): 1. ![t76](./media/t76.png) is a block included in coding box library. It reads the status value of the AD button in the coding box. Pressing is a high level (1), and releasing is a low level (0). Click on the ![](./media/t5.png) to switch buttons. ![t77](./media/t77.png) ## 3.6.4 Test Code You can manually build blocks, or directly open the code file we provide: `3-6-ADKey.ubp`. If you have any questions about how to open code files or upload code, please back to `1.9 Upload Code`. **Build code blocks:** 1. In ![](./media/control.png), drag ![](./media/t1.png) and ![](./media/t2.png) to the script area, and stack them together. ![t34](./media/t34.png) 2. In ![](./media/control.png), drag ![t52](./media/t52.png) block and put it into ![](./media/t2.png), and tap ![t53](./media/t53.png) to add some else-ifs as follows. ![t78](./media/t78.png) 2. In ![](./media/codingBox.png), drag 3 ![t76](./media/t76.png) and put it on the ![t79](./media/t79.png) of ![t52](./media/t52.png). Set the first to red, the second to yellow and the third to green. Tap ![t80](./media/t80.png) to remove “else”. ![t81](./media/t81.png) 3. In ![](./media/output.png), drag three ![t44](./media/t44.png) blocks and put them in ![t52](./media/t52.png). Press the red button to output “Press the red button”, yellow: “Press the yellow button” and green: “Press the green button”. **Complete code:** ![t82](./media/t82.png) ## 3.6.5 Test Result Connect the coding box to the MicroBlocks via USB or Bluetooth, and click ![t59](./media/t59.png) to upload the code to the coding box. Press the red button and it outputs `Press the red button`; Press the yellow button and it says `Press the yellow button`; Press the green button it outputs `Press the green button`. ![t83](./media/t83.png)